﻿using Model;
using Model.Xcode;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace Business
{
    public class EmployeeForDingTalkBll
    {
        public static void EmployeeInsertForDingTalk()
        {
            LogHelper log = LogFactory.GetLogger("EmployeeInsertForDingTalk");
            //查询所有人员         
            List<TSysUsers> UserList = TSysUsers.FindAll(TSysUsers._.ID>0).ToList();

            log.Info("\r\n------------------------------------------------人员数据导入到钉钉------------------------------------------------\r\n");

            #region 循环插入到钉钉
            foreach (var item in UserList)
            {
                try
                {
                    if (item.PersonnelStatus == "在岗")
                    {
                        InsertForDingTalk(item);
                    }
                    else
                    {
                        DeleteForDingTalk(item);
                    }
                }
                catch (Exception e)
                {
                    log.Error("\r\n EmployeeInsertForDingTalk() " + e.Message);
                }
            }

            //int d = 0;
            //foreach (var item in UserList)
            //{
            //    d = Edb.Deleteable<Tbiz_OperationTemp>().Where(it => it.ObjectId.Equals(item.UserId)).ExecuteCommand();
            //}
            #endregion
        }

        /// <summary>
        /// 插入人员数据到钉钉
        /// </summary>
        /// <param name="item"></param>
        public static void InsertForDingTalk(TSysUsers item)
        {
            LogHelper log = LogFactory.GetLogger("InsertForDingTalk");
            var findDepartment = DepartmentForDingTalkBll.GetDepartmentList().department.Where(a => a.sourceIdentifier == item.OrganizationId).ToList().FirstOrDefault();
            List<int> departmentList = new List<int>();
            if (findDepartment.id !="0")
            {
                departmentList.Add(findDepartment.id.ToInt());
            }

            EmployeeEntity model = new EmployeeEntity();
            model.userid = item.UserName;
            model.name = item.FullName;
            model.department = departmentList;
            model.position = item.Gwmc;
            model.mobile = item.Mobile;
            model.tel = item.PhoneContact;
            model.workPlace = item.BmmcPt;
            model.remark = item.DeptClassText;
            model.email = item.Email;
            model.jobnumber = item.EmployeeNo;
            model.isSenior = false;

            string param = model.ToJson();

            EmployeeResult Result = EmployeeBll.Create(param);
            if (Result != null)
            {
                if (Result.errcode == "0")
                {
                    //EmpList.Add(item);
                    //Console.Write("创建成功,UserId=" + Result.userid + "\r\n");
                }
                else
                {
                    //UserID在公司中已存在
                    if (Result.errcode == "60102")
                    {
                        string EmployeeJson = GetEmployee(item.UserName);
                        model = Newtonsoft.Json.JsonConvert.DeserializeObject<EmployeeEntity>(EmployeeJson);

                        model.userid = item.UserName;
                        model.name = item.FullName;
                        model.department = departmentList;
                        model.position = item.Gwmc;
                        model.mobile = item.Mobile;
                        model.tel = item.PhoneContact;
                        model.email = item.Email;
                        model.jobnumber = item.EmployeeNo;
                        param = model.ToJson();

                        Result res = EmployeeBll.Update(param);
                        if (res != null)
                        {
                            if (res.errcode == "0")
                            {
                                //EmpList.Add(item);
                                //Console.Write("更新成功," + Result.errmsg);
                            }
                            else
                            {
                                log.Error("\r\n EmployeeForDingTalkBll-InsertForDingTalk() " + Result.errmsg + "; UserId=" + item.UserName);
                                //Console.Write("\r\n" + Result.errmsg + "; UserId=" + item.UserId);
                            }
                        }
                        else
                        {
                            //Console.Write("无返回数据");
                        }
                    }
                    else
                    {
                        if (Result.errcode == "60104")
                        {
                            log.Error("\r\n EmployeeForDingTalkBll-InsertForDingTalk() " + Result.errmsg + "; UserId=" + item.UserName);
                        }
                        else if (Result.errcode == "40026")
                        {
                            //该外部联系人已存在
                            //AddEmployee(Edb, Ddb, item);
                            //EmpList.Add(item);
                        }
                        else
                        {
                            log.Error("\r\n EmployeeForDingTalkBll-InsertForDingTalk() " + Result.errmsg + "; UserId=" + item.UserName);
                        }
                        //Console.Write("\r\n" + Result.errmsg + "; UserId=" + item.UserId);
                    }
                }
            }
            else
            {
                //Console.Write("无返回数据");
            }
        }

        public static void DeleteForDingTalk(TSysUsers item)
        {
            LogHelper log = LogFactory.GetLogger("DeleteForDingTalk");

            Result Result = EmployeeBll.Delete(item.UserName);
            if (Result != null)
            {
                if (Result.errcode == "0")
                {
                    //EmpList.Add(item);
                    //Console.Write("删除成功," + Result.errmsg + "\r\n");
                }
                else
                {
                    log.Error("\r\n EmployeeForDingTalkBll-DeleteForDingTalk() " + Result.errmsg + "; UserId=" + item.UserName);
                    //Console.Write("\r\n" + Result.errmsg + "; UserId=" + item.UserId);
                }
            }
            else
            {
                //Console.Write("无返回数据");
            }
        }

        public static string GetEmployee(string userid)
        {
            LogHelper log = LogFactory.GetLogger("GetEmployee");
            string json = "";
            try
            {
                GetEmployee Result = EmployeeBll.Get(userid);

                if (Result != null)
                {
                    if (Result.errcode == "0")
                    {
                        json = Result.ToJson();
                        //Console.Write(json + "\r\n");
                    }
                    else if (Result.errcode == "90002")
                    {
                        System.Threading.Thread.Sleep(1500);
                        json = GetEmployee(userid);
                    }
                    else
                    {
                        if (Result.errcode != "60121")
                        {
                            //Console.Write(Result.errmsg);
                            log.Error("\r\n EmployeeForDingTalkBll-GetEmployee() " + Result.errmsg + "\r\n");
                        }
                        return "-1";
                    }
                }
                else
                {
                    //Console.Write("无返回数据");
                }
            }
            catch (Exception ex)
            {
                log.Error("\r\n EmployeeForDingTalkBll-GetEmployee() " + ex + "\r\n");
                //Console.Write(ex.Message);
            }
            return json;
        }

    }
}
